package appx.craft.droid;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import appx.craft.droid.utils.AssetsImageDownloader;

import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.MemoryCacheAware;
import com.nostra13.universalimageloader.cache.memory.impl.LRULimitedMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;

public class DroidApplication extends Application {

	@Override
	public void onCreate() {
		super.onCreate();
		
		/**
		 * On Application level init of image loading configuration
		 */
		initImageLoader(getApplicationContext());
	}
	public static void initImageLoader(Context mContext){
		
		int memoryCacheSize = (int) (Runtime.getRuntime().maxMemory() / 8);

		MemoryCacheAware<String, Bitmap> memoryCache;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			memoryCache = new LruMemoryCache(memoryCacheSize);
		} else {
			memoryCache = new LRULimitedMemoryCache(memoryCacheSize);
		}
		ImageLoaderConfiguration mConfig = new ImageLoaderConfiguration.Builder(mContext)
		.threadPoolSize(3)
		.threadPriority(Thread.NORM_PRIORITY - 2)
		.memoryCache(memoryCache)
		.denyCacheImageMultipleSizesInMemory()
		.tasksProcessingOrder(QueueProcessingType.LIFO)
		.discCacheFileNameGenerator(new Md5FileNameGenerator())
		.imageDownloader(new AssetsImageDownloader(mContext))
		.enableLogging() // Not necessary in common
		.build();

		// Initialize ImageLoader with configuration.
		ImageLoader.getInstance().init(mConfig);

	}
}
